package com.rla.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.rla.common.annotation.RepeatSubmit;
import com.rla.common.annotation.Log;
import com.rla.common.core.controller.BaseController;
import com.rla.common.core.domain.PageQuery;
import com.rla.common.core.domain.R;
import com.rla.common.utils.validate.AddGroup;
import com.rla.common.utils.validate.EditGroup;
import com.rla.common.enums.BusinessType;
import com.rla.common.utils.poi.ExcelUtil;
import com.rla.domain.vo.BaseHealthTargetRuleVo;
import com.rla.domain.bo.BaseHealthTargetRuleBo;
import com.rla.service.IBaseHealthTargetRuleService;
import com.rla.common.core.page.TableDataInfo;

/**
 * 健康数据指标规则
 *
 * @author rla
 * @date 2025-07-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/base/targetRule")
public class BaseHealthTargetRuleController extends BaseController {

    private final IBaseHealthTargetRuleService iBaseHealthTargetRuleService;

    /**
     * 查询健康数据指标规则列表
     */
    @GetMapping("/list")
    public TableDataInfo<BaseHealthTargetRuleVo> list(BaseHealthTargetRuleBo bo, PageQuery pageQuery) {
        return iBaseHealthTargetRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出健康数据指标规则列表
     */
    @Log(title = "健康数据指标规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseHealthTargetRuleBo bo, HttpServletResponse response) {
        List<BaseHealthTargetRuleVo> list = iBaseHealthTargetRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "健康数据指标规则", BaseHealthTargetRuleVo.class, response);
    }

    /**
     * 获取健康数据指标规则详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BaseHealthTargetRuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iBaseHealthTargetRuleService.queryById(id));
    }

    /**
     * 新增健康数据指标规则
     */
    @Log(title = "健康数据指标规则", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseHealthTargetRuleBo bo) {
        return toAjax(iBaseHealthTargetRuleService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改健康数据指标规则
     */
    @Log(title = "健康数据指标规则", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseHealthTargetRuleBo bo) {
        return toAjax(iBaseHealthTargetRuleService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除健康数据指标规则
     *
     * @param ids 主键串
     */
    @Log(title = "健康数据指标规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iBaseHealthTargetRuleService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
